package com.pdg.android.russianroulette;

import com.pdg.android.common.StateMachine;

import android.content.SharedPreferences;
import android.os.Bundle;

public class MainActivity extends StateMachine <GameData,MainActivity,GameState>{
	private static final String GAME_PREFERENCES_FILE = "russian_roulette_preferences";
	private SharedPreferences preferences;
	public MainActivity() {
		super(new GameData(),GameState.MAIN_MENU);
		putState(new MainMenuState(getData(),this));
		putState(new InstructionsState(getData(),this));
		putState(new AboutState(getData(),this));
		putState(new StatisticsState(getData(),this));
		putState(new RoundState(getData(),this));
		putState(new GameOverState(getData(),this));
		putState(new WinnerState(getData(),this));
	}

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setPreferences(this.getSharedPreferences(GAME_PREFERENCES_FILE, MODE_PRIVATE));
    }

	private void setPreferences(SharedPreferences thePreferences) {
		preferences = thePreferences;
	}
	
	private SharedPreferences getPreferences(){
		return preferences;
	}

	@Override
	protected void onSetCurrent(GameState theCurrent) {
		getData().setCurrentState(theCurrent);
	}
	@Override
    protected void onResume(){
    	super.onResume();
    	getData().load(getPreferences());
    	setCurrent(getData().getCurrentState());
    }

	@Override
    protected void onPause(){
    	super.onPause();
    	getData().commit(getPreferences());
    }
}